<template>
  <el-dialog :title="title" :visible="showDialog" @close="close">
    <el-form ref="addDept" :model="formData" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="部门名称">
        <el-input
          v-model="formData.name"
          placeholder="2-10个字符"
          style="width: 80%"
          size="mini"
        />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input
          v-model="formData.code"
          placeholder="2-10个字符"
          style="width: 80%"
          size="mini"
        />
      </el-form-item>
      <el-form-item prop="managerId" label="部门负责人">
        <el-select
          v-model="formData.managerId"
          placeholder="请选择负责人"
          style="width: 80%"
          size="mini"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input
          v-model="formData.introduce"
          placeholder="1-100个字符"
          type="textarea"
          size="mini"
          :rows="4"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button @click="btnOk" size="mini" type="primary"
              >确定</el-button
            >
            <el-button @click="close" size="mini">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  getDepartment,
  getManagerList,
  addDepartment,
  getDepartmentDetail,
  updataDepartment,
} from "@/api/department";
import { title } from "@/settings";
export default {
  name: "addDept",
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    currentNodeId: {
      type: Number,
    },
  },
  data() {
    return {
      managerList: [], //部门负责人列表
      formData: {
        code: "", // 部门编码
        introduce: "", // 部门介绍
        managerId: "", // 部门负责人id
        name: "", // 部门名称
        pid: "", // 父级部门的id
      },
      rules: {
        code: [
          { required: true, message: "请输入部门编码", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "部门编码的长度为2-10个字符",
            trigger: "blur",
          },
          {
            trigger: "blur",
            //校验添加的部门编码不能和已有的相同
            validator: async (rule, value, callback) => {
              let result = await getDepartment();
              if (this.formData.id) {
                //有id说明是编辑部门，需要排除自身项,要的是其他项
                result = result.filter((item) => {
                  item.id !== this.formData.id;
                });
              }
              if (result.some((item) => item.code === value)) {
                callback(new Error("已有该部门"));
              } else {
                callback();
              }
            },
          },
        ],
        introduce: [
          { required: true, message: "请输入部门介绍", trigger: "blur" },
          {
            min: 2,
            max: 100,
            message: "部门介绍的长度为2-100个字符",
            trigger: "blur",
          },
        ],
        managerId: [
          { required: true, message: "请输入部门负责人", trigger: "blur" },
        ],
        name: [
          { required: true, message: "请输入部门名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "部门名称的长度为2-10个字符",
            trigger: "blur",
          },
          {
            trigger: "blur",
            //校验添加的部门名称不能和已有的相同
            validator: async (rule, value, callback) => {
              let result = await getDepartment();
              if (this.formData.id) {
                //有id说明是编辑部门，需要排除自身项,要的是其他项
                result = result.filter((item) => {
                  item.id !== this.formData.id;
                });
              }
              if (result.some((item) => item.name === value)) {
                callback(new Error("已有该部门"));
              } else {
                callback();
              }
            },
          },
        ],
      },
    };
  },
  computed: {
    title() {
      return this.formData.id ? "编辑部门" : "新增部门";
    },
  },
  created() {
    this.getManagerList();
  },
  methods: {
    //1.关闭显示弹窗
    close() {
        this.formData={
        code: "", // 部门编码
        introduce: "", // 部门介绍
        managerId: "", // 部门负责人id
        name: "", // 部门名称
        pid: "", // 父级部门的id
      },
      this.$refs.addDept.resetFields();
      //通知父组件关闭
      this.$emit("update:showDialog", false);
    },
    //2.获取负责人下拉列表
    async getManagerList() {
      this.managerList = await getManagerList();
    },
    //3.点击确定整体校验
    btnOk() {
      this.$refs.addDept.validate(async (isOk) => {
        if (isOk) {
          //判断是新增部门还是编辑部门
          let msg = "新增";
          if (this.formData.id) {
            msg = "编辑";
            await updataDepartment(this.formData);
          } else {
            //校验成功调用接口
            await addDepartment({ ...this.formData, pid: this.currentNodeId });
          }
          //通知父组件进行更新
          this.$emit("updataDepartment");
          this.$message.success(`${msg}部门成功`);
          this.close();
        }
      });
    },
    //4.调用获取部门详情接口
    async getDepartmentDetail() {
      //部门详情的数据有id值，新增部门则没有
      this.formData = await getDepartmentDetail(this.currentNodeId);
    },
  },
};
</script>

<style>
</style>